package org.hdax.dm.rest.service;

import org.hdax.dm.dao.entitydao.DmSchedulerSeatPriceDao;
import org.hdax.dm.entity.scheduler_seat_price.DmSchedulerSeatPrice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@RestController
public class RestSchedulerSeatPriceService {

    @Autowired
    private DmSchedulerSeatPriceDao dmSchedulerSeatPriceDao;

    @PostMapping(path = "/findByDmSchedulerSeatPrice")
    public List<DmSchedulerSeatPrice> findByDmSchedulerSeatPrice(@RequestBody DmSchedulerSeatPrice dmSchedulerSeatPrice) {
        return dmSchedulerSeatPriceDao.findAll(where(dmSchedulerSeatPrice));
    }

    @PostMapping(path = "/getSchedulerSeatPriceBySchedulerIdAndArea")
    public DmSchedulerSeatPrice getSchedulerSeatPriceBySchedulerIdAndArea(@RequestParam("schedulerId") Integer schedulerId,
                                                                          @RequestParam("areaLevel") Integer areaLevel) {
        return dmSchedulerSeatPriceDao.findOne(new Specification<DmSchedulerSeatPrice>() {
            @Override
            public Predicate toPredicate(Root<DmSchedulerSeatPrice> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                //具体的条件判断
                if (schedulerId != null) {
                    predicates.add(criteriaBuilder.equal(root.get("scheduleId").as(Integer.class), schedulerId));
                }

                if (areaLevel != null) {
                    predicates.add(criteriaBuilder.equal(root.get("areaLevel").as(Integer.class), areaLevel));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
            }
        }).get();
    }

    public Specification<DmSchedulerSeatPrice> where(DmSchedulerSeatPrice dmSchedulerSeatPrice) {
        return new Specification<DmSchedulerSeatPrice>() {
            @Override
            public Predicate toPredicate(Root<DmSchedulerSeatPrice> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (dmSchedulerSeatPrice != null) {
                    if (dmSchedulerSeatPrice.getScheduleId() != null && dmSchedulerSeatPrice.getScheduleId() > 0) {
                        predicateList.add(criteriaBuilder.equal(root.get("scheduleId").as(Integer.class), dmSchedulerSeatPrice.getScheduleId()));
                    }
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[0]));
            }
        };
    }
}
